/*
 * @Author: szx
 * @Date: 2022-06-12 20:16:06
 * @LastEditTime: 2022-06-12 20:41:08
 * @Description:
 * @FilePath: \leetcode\面试题\16\26\26.js
 */
/**
 * @param {string} s
 * @return {number}
 */
 var calculate = function (s) {
    const stack = [];
    let num = 0,
        sign = '+';
    for (let i of s) {
        if (i === ' ') continue;
        if (i <= '9' && i >= '0') {
            num = num * 10 + parseInt(i);
            continue;
        }
        if (sign === '+') stack.push(num);
        else if (sign === '-') stack.push(-num);
        else if (sign === '*') stack.push(stack.pop() * num);
        else if (sign === '/') stack.push(Math.trunc(stack.pop() / num));
        sign = i;
        num = 0;
    }
    if (sign === '+') stack.push(num);
    else if (sign === '-') stack.push(-num);
    else if (sign === '*') stack.push(stack.pop() * num);
    else if (sign === '/') stack.push(Math.trunc(stack.pop() / num));
    return stack.reduce((a, b) => a + b, 0);
};
